package com.itboye.ihomebank.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.activity.me.ActivityCardRenZheng;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.PersonDataBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.util.controller.LoginController;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.SavePersonData;

import java.util.Observable;
import java.util.Observer;

/**
 * 房源管理
 */
public class ActivitySwitchRoles extends BaseOtherActivity implements
		OnClickListener ,Observer{

	View v_statusbar;
	private ImageView img_other, close_icon, img_shenfen;// 返回图片
	private TextView add_shap_title_tv;// 标题

	LinearLayout switch_layout01, switch_layout03;
	LinearLayout switch_layout02, li_shenfen;
	RelativeLayout jingjiren_renzheng;
	Button renzheng;
	private String brokerValidate;

	private String identity;
	protected String uid;
	UserPresenter userPresenter;
	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.switch_layout03:
			LoginController.onMySignClick(this, null);
			break;
		case R.id.close_icon:
			finish();
			break;
		case R.id.switch_layout01:// 我是房东
			intent = new Intent(MyApplcation.ctx, ActivityFaBuHouse.class);
			startActivity(intent);
			break;
		case R.id.switch_layout02:// 房源管理
			intent = new Intent(this, WodeFangyuanActivity.class);
			startActivity(intent);
			break;
		case R.id.renzheng:

		case R.id.img_shenfen:
			intent = new Intent(this, ActivityCardRenZheng.class);
			startActivity(intent);
			break;
		case R.id.li_shenfen:
			// if (brokerValidate != null) {
			// if(brokerValidate.equals("0")){
			intent = new Intent(this, ActivityCardRenZheng.class);
			startActivity(intent);
			// }
			// }
			break;

		default:
			break;
		}

	}
	@Override
	protected void onResume() {
		super.onResume();
		uid = SPUtils.get(this, null, SPContants.USER_ID, "")+ "";

		if (!(SPUtils.get(this, null,SPContants.BrokerValidate, "") + "").equals("1")){
			showProgressDialog("正在获取信息",false);
			userPresenter.getPersonData(uid);
		}else {

		}
	}
	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_switch_roles;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		setStatusBarHeight(v_statusbar);
		userPresenter = new UserPresenter(this);
		add_shap_title_tv.setText("我是房东");
		//身份识别属性
		brokerValidate = SPUtils.get(this, null, SPContants.BrokerValidate, "")
				+ "";
		if (brokerValidate != null) {
			if (brokerValidate.equals("0")) {// 经纪人未认证
				img_shenfen.setBackgroundResource(R.drawable.chahua);
			} else if (brokerValidate.equals("2")) {// 经纪人申请中
				img_shenfen.setBackgroundResource(R.drawable.shenqingzhong);
				renzheng.setVisibility(View.GONE);
			} else if (brokerValidate.equals("1")) {// 经纪人
				add_shap_title_tv.setText("我是经纪人");
				li_shenfen.setVisibility(View.GONE);
				img_shenfen.setBackgroundResource(R.drawable.chahua);
			}
		}

		identity = SPUtils.get(this, null,
				SPContants.IdentityValidate, "") + "";

		if (identity != null) {
			if (identity.equals("1")) {
				switch_layout03.setVisibility(View.GONE);
			} else {
				switch_layout03.setVisibility(View.VISIBLE);
			}
		}
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable o, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.persondata_success) {
				PersonDataBean bean = (PersonDataBean) entity.getData();
				SavePersonData.saveData(ActivitySwitchRoles.this, bean);

				String brokerValidateS=bean.getBrokerValidate();
				if (brokerValidateS.equals("0")) {// 经纪人未认证
					img_shenfen.setBackgroundResource(R.drawable.chahua);
				} else if (brokerValidateS.equals("2")) {// 经纪人申请中
					img_shenfen.setBackgroundResource(R.drawable.shenqingzhong);
					renzheng.setVisibility(View.GONE);
				} else if (brokerValidateS.equals("1")) {// 经纪人
					add_shap_title_tv.setText("我是经纪人");
					li_shenfen.setVisibility(View.GONE);
					img_shenfen.setBackgroundResource(R.drawable.chahua);
				}
			} else if (entity.getEventType() == UserPresenter.persondata_fail) {
				ByAlert.alert(entity.getData()+"");
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
		}
	}
}
